<?php

namespace app\models\common;

use think\facade\Validate;
use think\Model;
use think\facade\Db;

class SmsLogModel extends Model
{
    protected $connection = 'mongodb';
    protected $table = 'cm_sms_log';

    const STATUS_FAIL = 2;
    const STATUS_SUCCESS = 1;

    /**
     * 获取记录数量
     * @param array $param
     * @return mixed
     */
    public function getRecordCount(array $param = [])
    {
        $pdo = self::table($this->table);
        $pdo = $this->createSearchParam($pdo, $param);
        $count = $pdo->count();
        return $count;
    }

    /**
     * 创建筛选条件
     * @param $pdo
     * @param array $param
     * @return mixed
     */
    private function createSearchParam($pdo, array $param = [])
    {
        if (isset($param['mobile'])) {
            $pdo->where('mobile', $param['mobile']);
        }
        if (isset($param['api_key']) and $param['api_key']) {
            $pdo->where('api_key', $param['api_key']);
        }
        if (isset($param['start_time'][0])) {
            $pdo->where('create_time', '>=', $param['start_time']);
        }
        if (isset($param['end_time'][0])) {
            $pdo->where('create_time', '<=', $param['end_time']);
        }
        return $pdo;
    }

    /**
     * 创建记录
     * @param array $param
     * @return array
     * @date 2021-01-21 13:47
     */
    public function createRecord(array $param = [])
    {
        $checkRes = $this->checkSaveData($param);
        if ($checkRes['status'] != STATUS_CODE_SUCCESS) {
            return $checkRes;
        }
        $data = $checkRes['data'];
        $data['create_time'] = date('Y-m-d H:i:s');
        $res = self::insert($data);
        if (!$res) {
            return funReturn(STATUS_CODE_INSIDEERROR, '生成失败');
        }
        return funReturn(STATUS_CODE_SUCCESS, 'success');
    }


    /**
     * 检查需要保存的数据
     * @param array $params
     * @return array
     * @date 2021-01-21 13:45
     */
    private function checkSaveData(array $params = [])
    {
        $rule = [
            'mobile' => 'require|mobile',
            'content' => "require",
            'api_key' => 'require',
            'status' => 'require|in:' . self::STATUS_SUCCESS . ',' . self::STATUS_FAIL,
        ];
        $message = [
            'mobile.require' => '缺少手机号',
            'mobile.mobile' => '手机号非法',
            'content.require' => '缺少短信内容',
            'api_key.require' => '缺少api_key',
            'status.require' => '缺少status',
            'status.in' => 'status非法',
        ];
        $validate = Validate::message($message)->rule($rule);
        if (!$validate->check($params)) {
            return funReturn(STATUS_CODE_DATAERROR, $validate->getError());
        }
        $data = [
            'api_key' => (string)$params['api_key'],
            'mobile' => (string)$params['mobile'],
            'content' => (string)$params['content'],
            'status' => (int)$params['status']
        ];
        return funReturn(STATUS_CODE_SUCCESS, 'success', $data);
    }
}